package edu.gatech.cs2340.teamrocket.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import edu.gatech.cs2340.teamrocket.control.exception.InvalidNameException;
import edu.gatech.cs2340.teamrocket.control.guicontrol.IntroScreenControl;
import edu.gatech.cs2340.teamrocket.model.job.Job;
import edu.gatech.cs2340.teamrocket.model.team.Team;

@SuppressWarnings("serial")
public class IntroScreenGUI extends JPanel {

    private static final String validName = "^[A-Za-z0-9_][A-Za-z0-9_\\s]{0,19}$";

    private static String[] names = {
        "Pikachu",
        "Charizard",
        "Venosaur",
        "Blastoise",
        "Lapras",
        "Snorlax"
    };

    private final JTextField playerName, textfields[];
    // why are these not initialized inside the constructor? who knows...
    private final ButtonGroup jobGroup = new ButtonGroup();
    private final IntroScreenControl introScreenControl;
    private final JButton eliteButton = new JButton(new ImageIcon("img/intro_graphics/elite.png"));
	private final ImageIcon eliteclicked = new ImageIcon("img/intro_graphics/eliteclicked.png");
    private final JButton gymLeaderButton = new JButton(new ImageIcon("img/intro_graphics/gym.png"));
	private final ImageIcon gymclicked = new ImageIcon("img/intro_graphics/gymclicked.png");
    private final JButton trainerButton = new JButton(new ImageIcon("img/intro_graphics/trainer.png"));
	private final ImageIcon trainerclicked = new ImageIcon("img/intro_graphics/trainerclicked.png");
    private final Image bg = new ImageIcon("img/intro_graphics/bg.png").getImage();
    private final JLabel lblJob, yourName, pokemonName;
    private final JButton btnBeginYourJourney;
	private final ImageIcon begin = new ImageIcon("img/intro_graphics/begin.png");
	private final ImageIcon beginroll = new ImageIcon("img/intro_graphics/beginrollover.png");
	private final ImageIcon back = new ImageIcon("img/intro_graphics/back.png");
	private final ImageIcon backroll = new ImageIcon("img/intro_graphics/backrollover.png");

    /**
     * Create the panel.
     */
    public IntroScreenGUI(IntroScreenControl aControl) {
        //set background
        /*
         * Dimension size = new Dimension(bg.getWidth(null), bg.getHeight(null));
         * setPreferredSize(size);
         * setMinimumSize(size);
         * setMaximumSize(size);
         * setSize(size);
         * //*/

        setLayout(null);
        lblJob = new JLabel("<html><p color = green> Elite Master</p></html>");
        lblJob.setBounds(744, 371, 400, 16);
        add(lblJob);
        introScreenControl = aControl;
        jobGroup.add(eliteButton);
        jobGroup.add(gymLeaderButton);
        jobGroup.add(trainerButton);
        eliteButton.setSelectedIcon(eliteclicked);
        eliteButton.setSelected(true);
        eliteButton.setOpaque(true);
        eliteButton.setBorderPainted(false);
        eliteButton.setBackground(new Color(0,0,0,0));
        eliteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                lblJob.setText("<html><p color = green> Elite Master</p></html>");
                eliteButton.setSelected(true);
                gymLeaderButton.setSelected(false);
                trainerButton.setSelected(false);
            }
        });
        
        eliteButton.setBounds(339, 371, 110, 80);
        add(eliteButton);
        gymLeaderButton.setSelectedIcon(gymclicked);
        gymLeaderButton.setOpaque(true);
        gymLeaderButton.setBorderPainted(false);
        gymLeaderButton.setBackground(new Color(0,0,0,0));
        gymLeaderButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                lblJob.setText("<html><p color = green> Gym Leader</p></html>");
                eliteButton.setSelected(false);
                gymLeaderButton.setSelected(true);
                trainerButton.setSelected(false);
            }
        });
        gymLeaderButton.setBounds(339, 462, 110, 80);
        add(gymLeaderButton);
        
        trainerButton.setSelectedIcon(trainerclicked);
        trainerButton.setOpaque(true);
        trainerButton.setBorderPainted(false);
        trainerButton.setBackground(new Color(0,0,0,0));
        trainerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                lblJob.setText("<html><p color = green> Trainer</p></html>");
                eliteButton.setSelected(false);
                gymLeaderButton.setSelected(false);
                trainerButton.setSelected(true);
                trainerButton.setBackground(new Color(0,0,0,0));
            }
        });
        trainerButton.setBounds(339, 553, 110, 80);
        add(trainerButton);

        JLabel lblEliteMaster = new JLabel("<html>You begin your journey with strong pokemon and plenty of supplies! " +
                "An Elite Master begins with the most resources, but will get a lower score.</html>");
        lblEliteMaster.setBounds(464, 371, 245, 80);
        add(lblEliteMaster);

        JLabel lblGymleader = new JLabel("<html>You begin your journey with fairly strong pokemon and enough supplies. " +
                "A Gym Leader begins with moderate resources.</html>");
        lblGymleader.setBounds(464, 462, 245, 80);
        add(lblGymleader);

        JLabel lblTrainer = new JLabel("<html>You begin your journey with only the allowance your mom gave you. " +
                "A Trainer begins with very little resources, but might get a higher score.</html>");
        lblTrainer.setBounds(464, 553, 245, 80);
        add(lblTrainer);

        yourName = new JLabel("<html><p color = black>Please enter your name:</p></html>");
        yourName.setBounds(69, 350, 250, 14);
        add(yourName);

        playerName = new JTextField();
        playerName.setText("Red");
        playerName.setBounds(69, 369, 200, 20);
        playerName.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                if (isValid(playerName.getText())){
                    playerName.setForeground(Color.black);
                    btnBeginYourJourney.setEnabled(true);
                }
                else{
                    playerName.setForeground(Color.red);
                    btnBeginYourJourney.setEnabled(false);
                }

            }
        });
        add(playerName);
        playerName.setColumns(12);

        pokemonName = new JLabel("<html><p color = black>Set the names of your Pokemon:</p></html>");
        pokemonName.setBounds(69, 403, 250, 16);
        add(pokemonName);

        textfields = new JTextField[Team.NUM_TRAVELERS];
        for(int i = 0; i < Team.NUM_TRAVELERS; i++){
            textfields[i] = new JTextField();
            textfields[i].setText(names[i]);
            textfields[i].setBounds(69, 431 + (30 * i), 200, 20); // move 30 units down each time

            final int _i = i;

            textfields[i].addKeyListener(new KeyAdapter() {

                final int index = _i;

                public void keyReleased(KeyEvent e) {
                    if (!isValid(textfields[index].getText())){
                        textfields[index].setForeground(Color.red);
                        btnBeginYourJourney.setEnabled(false);
                    }
                    else{
                        textfields[index].setForeground(Color.black);
                        for(int i = 0; i < Team.NUM_TRAVELERS; i++){
                            if(!isValid(textfields[i].getText()))
                                return;
                        }
                        btnBeginYourJourney.setEnabled(true);
                    }

                }
            });
            add(textfields[i]);
            textfields[i].setColumns(20);
        }

        btnBeginYourJourney = new JButton(begin);
        btnBeginYourJourney.setRolloverIcon(beginroll);
        btnBeginYourJourney.setOpaque(true);
        btnBeginYourJourney.setBorderPainted(false);
        btnBeginYourJourney.setBackground(new Color(0,0,0,0));
        btnBeginYourJourney.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                introScreenControl.begin();
            }
        });
        btnBeginYourJourney.setBounds(731, 499, 200, 43);
        add(btnBeginYourJourney);

        JButton btnBackToMenu = new JButton(back);
        btnBackToMenu.setRolloverIcon(backroll);
        btnBackToMenu.setOpaque(true);
        btnBackToMenu.setBorderPainted(false);
        btnBackToMenu.setBackground(new Color(0,0,0,0));
        btnBackToMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                introScreenControl.backToMenu();
            }
        });
        btnBackToMenu.setBounds(731, 558, 200, 43);
        add(btnBackToMenu);

        JLabel lblPickAJob = new JLabel("Pick a Job:");
        lblPickAJob.setBounds(341, 349, 132, 16);
        add(lblPickAJob);
        JLabel lblYourCurrentlySelected = new JLabel("Your Currently Selected Profession:");
        lblYourCurrentlySelected.setBounds(734, 350, 245, 14);
        add(lblYourCurrentlySelected);


    }

    public Job getSelectedJob(){
        if (eliteButton.isSelected()){
            return Job.EliteMaster;
        }else if(gymLeaderButton.isSelected()){
            return Job.GymLeader;
        }else{
            return Job.Trainer;
        }
    }

    /**
     * A method checking a string is valid
     * @param aString
     * @return true if the string is valid, false if it is not.
     */
    public boolean isValid(String aString){
        return aString.matches(validName);
    }

    /**
     * Retrieves the name of the player the user inputs and validates it
     * @return the name of the player
     */
    public String getPlayerName(){
        if (!playerName.getText().matches(validName)){
            throw new InvalidNameException();
        }
        else{
            return playerName.getText();
        }
    }

    /**
     * Retrieves the name of traveler i the user inputs and validates it
     * @return the name of the traveler 1
     */
    public String getTravelerName(int i){
        if (!textfields[i].getText().matches(validName)){
            throw new InvalidNameException();
        }
        else{
            return textfields[i].getText();
        }
    }

    /**
     * paint the backgroud image
     */
    public void paintComponent(Graphics g){
    	g.setColor(Color.white);
    	g.fillRect(0, 0, 1024, 768);
        g.drawImage(bg, 0, 0, null, null);
    }
}